package com.briup.jz.service.impl;

import com.briup.jz.bean.AccountCustomer;
import com.briup.jz.bean.AccountCustomerExample;
import com.briup.jz.bean.extend.AccountCustomerExtend;
import com.briup.jz.dao.AccountCustomerMapper;
import com.briup.jz.dao.extend.AccountCustomerExtendMapper;
import com.briup.jz.service.IAccountCustomerService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: jz
 * @description: 顾客业务实现类
 * @author: catmilko
 * @create: 2020/6/12 15:57
 **/
@Service
public class AccountCustomerServiceImpl implements IAccountCustomerService {
    @Resource
    private AccountCustomerMapper accountCustomerMapper;
    @Resource
    private AccountCustomerExtendMapper accountCustomerExtendMapper;

    @Override
    public List<AccountCustomer> findAll() {
        return accountCustomerMapper.selectByExample(new AccountCustomerExample());
    }

    @Override
    public AccountCustomer findById(long id) {
        return accountCustomerMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(AccountCustomer accountCustomer) throws CustomerException {
        if (accountCustomer.getId() == null) {
            accountCustomerMapper.insert(accountCustomer);
        } else {
            if (accountCustomerMapper.selectByPrimaryKey(accountCustomer.getId()) == null) {
                throw new CustomerException("修改失败，数据不存在");
            }
            accountCustomerMapper.updateByPrimaryKey(accountCustomer);
        }
    }

    @Override
    public void deleteById(long id) throws CustomerException {
        if (accountCustomerMapper.selectByPrimaryKey(id) == null) {
            throw new CustomerException("删除失败，数据不存在");
        }
        accountCustomerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<AccountCustomerExtend> findAllWithOrderAndUser() {
        return accountCustomerExtendMapper.selectAllWithOrderAndUser();
    }
}
